//
//  AssetDetailViewModel.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/7/18.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class AssetDetailViewModel: BaseViewModel {
    @objc dynamic var refreshBool:Bool = false // kvo监听
    var model:AssetModel = AssetModel() //传递的model
    var type:CoinAccountType?
    var dataArr:[CoinListModel] = []
    var pageNo:Int = 1
    let pageSize:Int = 10
    var currentType:String = ""
    
    lazy var legalRequest: CashFlowRequest = {
        let api = CashFlowRequest()
        api.type = type
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    /// 获取资金流水
    func archiveInformation() {
        self.request(request: legalRequest, success: {[weak self] (result) in
            if self?.pageNo == 1 {
                self?.dataArr.removeAll()
            }
            let dic:[String:Any]? = result != nil ? result : nil
            if dic == nil {
                return
            }
            if dic?["data"] as? [String : Any] == nil  {
                return
            }
            guard let data = dic?["data"] as? [String : Any] else {
                return
            }
           
            let model  = CoinTransModel(JSON: data)
            if model?.list == nil {
                return
            }
            for smallModel in (model?.list)! {
                self?.dataArr.append(smallModel)
            }
             self?.refreshBool = true
            
        }) {[weak self] (reponse) in
            if self?.pageNo == 1 {
                self?.dataArr.removeAll()
            }
        }
    }
}

extension AssetDetailViewModel: RequestCallBack,RequestDataReformer,RequestParamSource {
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if successBlock != nil {
            successBlock!(data)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if failBlock != nil {
            failBlock!(request)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? CashFlowRequest {
            pramas.updateValue(pageNo, forKey: "pageNo")
            pramas.updateValue(pageSize, forKey: "pageSize")
            pramas.updateValue(model.coinCode, forKey: "coinCode")
            let number = ProfileManager.getCodeWithTransInfo(name: currentType, type: type!)
            if number != nil {
                pramas.updateValue(number!, forKey: "handleType")
            }
        }
        return pramas
    }
    
    
    
}
